package com.example.skywalking.dto;

import lombok.Data;
import org.slf4j.MDC;

@Data
public class Response<T> {

    public static final String SUCCESS_CODE = "0000";
    public static final String SUCCESS_MSG = "成功";
    public static final String FAILURE_CODE = "9999";
    public static final String FAILURE_MSG = "失败";

    private String code;
    private String msg;
    private T data;
    private String requestId;

    public Response(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.requestId = MDC.get("requestId");
    }

    public Response(String code, String msg) {
        this.code = code;
        this.msg = msg;
        this.requestId = MDC.get("requestId");
    }

    public Response(T data) {
        this.code = SUCCESS_CODE;
        this.msg = SUCCESS_MSG;
        this.data = data;
        this.requestId = MDC.get("requestId");
    }


    public static Response success() {
        return new Response(SUCCESS_CODE, SUCCESS_MSG);
    }

    public static <T> Response<T> success(T data) {
        return new Response(data);
    }

    public static Response fail(String code, String msg) {
        return new Response(code, msg);
    }
}